package org.cst8277.ums.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.cst8277.ums.entity.Role;
import org.cst8277.ums.service.IRoleService;
import org.cst8277.ums.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ums/role")
public class UmsRolesController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("getRoles/{pageNum}/{pageSize}")
    public Mono<ResponseEntity<Map<String, Object>>> getRoles(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Map<String, Object> response = new HashMap<>();
        log.info("获取所有角色");
        Page<Role> page = roleService.page(new Page<Role>(pageNum, pageSize));
        if (page == null || page.getTotal() == 0) {
            response.put(Constants.CODE, "500");
            response.put(Constants.MESSAGE, "Roles have not been retrieved");
            response.put(Constants.DATA, new HashMap<>());
        } else {
            response.put(Constants.CODE, "200");
            response.put(Constants.MESSAGE, "List of Roles has been requested successfully");
            response.put(Constants.DATA, page.getOrders());
        }
        return Mono.just(ResponseEntity.ok().header(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON)
                .header(Constants.ACCEPT, Constants.APPLICATION_JSON).body(response));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/addRole/role", consumes = Constants.APPLICATION_JSON)
    public Mono<ResponseEntity<Map<String, Object>>> createUser(@RequestBody Role role) {
        Map<String, Object> response = new HashMap<>();
        log.info("开始创建角色, role:{}", role);
        role.setUpdateTime(new Date());
        role.setCreateTime(new Date());
        role.setUpdator(Constants.DEFAULT_UPDATOR);

        boolean result = roleService.save(role);
        if (result == false) {
            response.put(Constants.CODE, "500");
            response.put(Constants.MESSAGE, "Role has not been created");
            response.put(Constants.DATA, "Check email for duplicates first");
        } else {
            response.put(Constants.CODE, "201");
            response.put(Constants.MESSAGE, "Role created");
            response.put(Constants.DATA, role.getId());
        }
        return Mono.just(ResponseEntity.ok().header(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON)
                .header(Constants.ACCEPT, Constants.APPLICATION_JSON).body(response));
    }

    @RequestMapping(method = RequestMethod.GET, path = "/getRole/{role-id}")
    public Mono<ResponseEntity<Map<String, Object>>> getUser(@PathVariable(value = "role-id", required = true) long roleId) {
        log.info("查找角色, roleId:{}", roleId);
        Map<String, Object> response = new HashMap<>();
        Role role = roleService.getById(roleId);
        if (role.getId() == null) {
            response.put(Constants.CODE, "404");
            response.put(Constants.MESSAGE, "Role have not been found");
            response.put(Constants.DATA, new Role());
        } else {
            response.put(Constants.CODE, "200");
            response.put(Constants.MESSAGE, "Role has been retrieved successfully");
            response.put(Constants.DATA, role);
        }
        return Mono.just(ResponseEntity.ok().header(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON)
                .header(Constants.ACCEPT, Constants.APPLICATION_JSON).body(response));
    }
}
